<template>
	<view>
		<view class="main">
			<u-form :model="form" label-width="160" ref="uForm">
				<u-form-item label="头像">
					<u-avatar :src="src" size="120"></u-avatar>
				</u-form-item>
				<u-form-item label="昵称" prop="nickName">
					<u-input v-model="form.nickName"></u-input>
				</u-form-item>
				<u-form-item label="性别">
					<u-radio-group v-model="radio" @change="radioGroupChange">
									<u-radio v-for="(item, index) in radioList" :key="index" :name="item.name" :disabled="item.disabled">
										{{ item.name }}
									</u-radio>
								</u-radio-group>
				</u-form-item>
				<u-form-item label="用户名">
					<u-input v-model="form.userName" disabled></u-input>
				</u-form-item>
				<u-form-item label="联系电话" prop="phonenumber">
					<u-input v-model="form.phonenumber"></u-input>
				</u-form-item>
				<u-form-item label="身份证">
					<u-input v-model="form.idCard" disabled></u-input>
				</u-form-item>
				<u-form-item label="邮箱">
					<u-input v-model="form.email" disabled></u-input>
				</u-form-item>
			</u-form>
		</view>
		<button @click="submit">修改信息</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form: {
					avatar: '',
					email: '',
					idCard: '',
					nickName: '',
					phonenumber: '',
					sex: ''
				},
				src: '',
				radioList: [
					{
						name: '男',
						disabled: false
					},
					{
						name: '女',
						disabled: false
					}
				],
				radio: '',
				rules: {
					nickName: [
						{
							required: true,
							message: '昵称不能为空',
							trigger: ['blur','change']
						}
					],
					phonenumber: [
						{
							required: true,
							message: '昵称不能为空',
							trigger: ['blur','change']
						},
						{
							min: 11,
							max: 11,
							message: '手机号格式错误',
							trigger: ['blur','change']
						}
					]
				}
			};
		},
		onLoad() {
			this.getUser()
		},
		methods: {
			getUser(){
				this.$request({
					url: '/prod-api/api/common/user/getInfo',
				}).then(res => {
					this.form = res.user
					console.log(this.form)
					if(this.form.sex === '1'){
						this.radio = '男'
					}else{
						this.radio = '女'
					}
					this.$refs.uForm.setRules(this.rules)
				})
			},
			radioGroupChange(e){
				this.radio = e
				console.log(this.radio)
			},
			submit(){
				this.$refs.uForm.validate(valid => {
					if(valid){
						this.$request({
							url: '/prod-api/api/common/user',
							method: 'PUT',
							data: {
								...this.form
							}
						}).then(res => {
							if(res.code === 200){
								uni.showToast({
									title:'修改成功'
								})
								setTimeout(() => {
									uni.switchTab({
										url:'/pages/user/user'
									})
								}, 1000)
							}else{
								uni.showToast({
									icon: 'error',
									title:res.msg
								})
							}
						})
					}
				})
			}
		}
		// onReady() {
		// 	this.$refs.uForm.setRules(this.rules)
		// }
	}
</script>

<style lang="scss">
view{
	padding: 20rpx;
	.main{
		padding: 20rpx;
		u-form{
			padding: 20rpx;
		}
	}
	button{
		background-color: #2979ff;
		color: white;
		width: 200rpx;
		height: 80rpx;
		display: flex;
		justify-content: center;
		align-items: center;
	}
}
</style>
